﻿using System;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Data.EffiProz;


namespace EffiProz.Test
{
    /// <summary>
    /// Summary description for TestNullInUnion
    /// </summary>
    [TestClass]
    public class TestNullInUnion
    {
        public TestNullInUnion()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        //
        // You can use the following additional attributes as you write your tests:
        //
        // Use ClassInitialize to run code before running the first test in the class
        // [ClassInitialize()]
        // public static void MyClassInitialize(TestContext testContext) { }
        //
        // Use ClassCleanup to run code after all tests in a class have run
        // [ClassCleanup()]
        // public static void MyClassCleanup() { }
        //
        // Use TestInitialize to run code before running each test 
        // [TestInitialize()]
        // public void MyTestInitialize() { }
        //
        // Use TestCleanup to run code after each test has run
        // [TestCleanup()]
        // public void MyTestCleanup() { }
        //
        #endregion

        [TestMethod]
        public void NulLUnion()
        {
            string connString = "Connection Type=Memory ; Initial Catalog=NullUnion; User=sa; Password=;";
            using (DbConnection conn = new EfzConnection(connString))
            {
                conn.Open();

                DbCommand command = conn.CreateCommand();
                command.CommandText = "CREATE TABLE t1 (id int not null, v1 int, v2 int, primary key(id));";
                command.ExecuteNonQuery(); ;

                command.CommandText = "CREATE TABLE t2 (id int not null, v1 int, v3 int, primary key(id));";
                command.ExecuteNonQuery();

                command.CommandText = "INSERT INTO t1 values(1,1,1); INSERT INTO t1 values(2,2,2); INSERT INTO t2 values(1,3,3)";
                command.ExecuteNonQuery();

                command.CommandText = "select t as atable, a as idvalue, b as value1, c as value2, d as value3 from("
            + "(select 't1' as t, t1.id as a, t1.v1 as b, t1.v2 as c, null as d from t1) union"
            + "(select 't2' as t, t2.id as a, t2.v1 as b, null as c, t2.v3 as d from t2)) order by atable, idvalue";

                EfzDataReader rs = (EfzDataReader)command.ExecuteReader();

                               
                Assert.IsTrue(rs.Read());
                Assert.AreEqual("t1", rs.GetString(rs.GetOrdinal( "atable")));
                Assert.AreEqual(1, rs.GetInt32(rs.GetOrdinal("idvalue")));
                Assert.AreEqual(1, rs.GetInt32(rs.GetOrdinal("value1")));
                Assert.AreEqual(1, rs.GetInt32(rs.GetOrdinal("value2")));
                Assert.IsTrue(rs.IsDBNull(rs.GetOrdinal("value3")));
                Assert.IsTrue(rs.Read());
                Assert.AreEqual("t1", rs.GetString(rs.GetOrdinal("atable")));
                Assert.AreEqual(2, rs.GetInt32(rs.GetOrdinal("idvalue")));
                Assert.AreEqual(2, rs.GetInt32(rs.GetOrdinal("value1")));
                Assert.AreEqual(2, rs.GetInt32(rs.GetOrdinal("value2")));
                Assert.IsTrue(rs.IsDBNull(rs.GetOrdinal("value3")));
                Assert.IsTrue(rs.Read());
                Assert.AreEqual("t2", rs.GetString(rs.GetOrdinal("atable")));
                Assert.AreEqual(1, rs.GetInt32(rs.GetOrdinal("idvalue")));
                Assert.AreEqual(3, rs.GetInt32(rs.GetOrdinal("value1")));
                Assert.AreEqual(3, rs.GetInt32(rs.GetOrdinal("value3")));
                Assert.IsTrue(rs.IsDBNull(rs.GetOrdinal("value2")));

                Assert.IsFalse(rs.Read());
            }
        }
    }
}
